package com.leadingsoft.pwxk.services.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.leadingsoft.pwxk.model.Summary;
import com.leadingsoft.pwxk.repository.SummaryRepository;
import com.leadingsoft.pwxk.services.AbstractService;
import com.leadingsoft.pwxk.services.SummaryService;

@Service
public class SummaryServiceImpl extends AbstractService<Summary> implements SummaryService {

	@Autowired
	SummaryRepository summaryRepository;

	@Override
	public void saveAll(List<Summary> models) {
		summaryRepository.saveAll(models);
	}

	@Override
	public void deleteAll() {
		summaryRepository.deleteAll();
	}

	@Override
	public String findLatestTime() {
		Sort sort = new Sort(Direction.DESC, "itemendtime");
		List<Summary> findAll = summaryRepository.findAll(sort);
		if (!findAll.isEmpty() && findAll.size() > 0) {
			return findAll.get(0).getItemendtime();
		}
		return null;
	}

	@Override
	public Long getCount() {
		return summaryRepository.count();
	}

	@Override
	public void save(Summary model) {
		summaryRepository.save(model);
	}

	@Override
	public List<Summary> getSyncList(String itemendtime) {
		Sort sort = new Sort(Direction.ASC, "itemendtime");
		if (null == itemendtime) {
			return summaryRepository.findAll(sort);
		}
		return summaryRepository.findByItemendtimeAfterOrderByItemendtimeAsc(itemendtime);
	}
}
